﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

public partial class TypeOfDeliver : System.Web.UI.Page
{
    SqlConnection cnn;
    SqlCommand cmd;
    SqlDataReader dread;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check user login or not
        if (Session["Username"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
            if (Session["RoleName"].ToString() == "Employee")
            {
                Response.Redirect("ValidateUser.aspx");
            }

        cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        cnn.Open();
        cmd = new SqlCommand("SELECT DTypeID, DTypeName, Description, DTypeCharge FROM TypeOfDeliver", cnn);
        dread = cmd.ExecuteReader();
        if (!IsPostBack)
        {
            gvTypeOfDeliver.DataSource = dread;
            gvTypeOfDeliver.DataBind();
        }
        dread.Close();
    }

    protected void gvTypeOfDeliver_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string DTID;
        DTID = Convert.ToString(e.CommandArgument);

        if (e.CommandName == "DTUpdate")
        {
            Response.Redirect("TypeOfDeliver_Update.aspx?DTID=" + DTID);
        }

        if (e.CommandName == "DTDelete")
        {
            cmd = new SqlCommand("Delete from TypeOfDeliver where DTypeID = '" + DTID + "'", cnn);
            dread = cmd.ExecuteReader();
            pageload();
        }
    }
    protected void imgbSearch_Click(object sender, ImageClickEventArgs e)
    {
        cmd = new SqlCommand("SELECT DTypeID, DTypeName, Description, DTypeCharge FROM TypeOfDeliver WHERE DTypeName like '%" + txtDTName.Text + "%'", cnn);
        dread = cmd.ExecuteReader();
        if (txtDTName.Text == "")
        {
            gvTypeOfDeliver.DataSource = null;
            gvTypeOfDeliver.DataBind();
        }
        else
        {
            gvTypeOfDeliver.DataSource = dread;
            gvTypeOfDeliver.DataBind();
        }
    }

    protected void imgbShowAll_Click(object sender, ImageClickEventArgs e)
    {
        pageload();
    }

    protected void pageload()
    {
        dread.Close();
        cmd = new SqlCommand("SELECT DTypeID, DTypeName, Description, DTypeCharge FROM TypeOfDeliver", cnn);
        dread = cmd.ExecuteReader();
        gvTypeOfDeliver.DataSource = dread;
        gvTypeOfDeliver.DataBind();
    }

}